<?php

Raise::load('core.RaiseSession');

/**
 * TestCoreRaiseSession class
 * Testing unit of 'phpraise/core/RaiseSession.php'
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseSession extends RaiseTest {
    
    public static function testClass(){
        self::assertClassExists('RaiseSession');
        self::assertClassExtends('RaiseSession', 'RaiseHelper');
        self::assertConstantDefined('RaiseSession::DEFAULT_SESSION_NAME');
        self::assertConstantDefined('RaiseSession::SIGNATURE_KEY');
        self::assertConstantDefined('RaiseSession::INTERNAL_EXPIRY_KEY');
    }

    public static function testSession(){
        $session = new RaiseSession();
        self::assertStrictEqual($session->name(), RaiseSession::DEFAULT_SESSION_NAME);
        $session->name('testSession');
        self::assertStrictEqual($session->name(), 'testSession');

        self::assertType($session->id(), 'string');
        $sessionId = session_id();
        self::assertStrictEqual($session->id(), $sessionId);
        $session->antiFixation();
        self::assertNotEqual($sessionId, $session->id());

        $session->start();

        $data = 'testData';
        $session->set('test', $data);
        self::assertStrictEqual($session->get('test'), $data);
        $session->remove('test');
        self::assertStrictEqual($session->get('test'), null);

        // WARNING: the following piece of code will crash the server, somehow
//        try{
//            $app = new RaiseApplication(new RaiseSettings());
//            $session->application($app);
//            self::assert($session->application());
//        }catch(Exception $e){
//            self::addError('An error has been raised while adding a valid RaiseApplication. ' . $e->getMessage());
//        }

        try{
            $session->application('fail now!');
            self::addError('An error has not been raised while adding a invalid RaiseApplication. ');
        }catch(Exception $e){
            self::assert($e);
        }
    }

}
